package com.pinyougou.sellergoods.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.common.pojo.PageResult;
import com.pinyougou.mapper.SpecificationMapper;
import com.pinyougou.mapper.SpecificationOptionMapper;
import com.pinyougou.pojo.Specification;
import com.pinyougou.pojo.SpecificationOption;
import com.pinyougou.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

//导入实现类的接口
@Service(interfaceName = "com.pinyougou.service.SpecificationService")
@Transactional
public class SpecificationServiceImpl implements SpecificationService {
    //注入规格
    @Autowired
    private SpecificationMapper specificationMapper;
   //注入规格选项
    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;


    /**
     * 添加数据
     *
     * @param specification
     */
    @Override
    public void save(Specification specification) {
        try {
            //保存规格
            // 1. 往规格表插入数据(会返回主键id)
            specificationMapper.insertSelective(specification);
            //保存规格选项
            specificationOptionMapper.save(specification);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 修改方法
     *
     * @param specification
     */
    @Override
    public void update(Specification specification) {
        try{
            // 修改往规格表数据
            specificationMapper.updateByPrimaryKeySelective(specification);
            /**########### 修改规格选项表数据 ###########*/
            // 第一步：删除规格选项表中的数据 spec_id
            // delete from tb_specification_option where spec_id = ?
            // 创建规格选项对象，封装删除条件 通用Mapper
          /*  SpecificationOption so = new SpecificationOption();
            so.setSpecId(specification.getId());
            specificationOptionMapper.delete(so);
            // 第二步：往规格选项表插入数据
            specificationOptionMapper.save(specification);*/
        }catch (Exception ex){
            throw new RuntimeException(ex);
        }


    }

    /**
     * 根据主键id删除
     *
     * @param id
     */
    @Override
    public void delete(Serializable id) {

    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteAll(Serializable[] ids) {

        try{
            for (Serializable id : ids){
                //根据id删除数据
                SpecificationOption so = new SpecificationOption();
                so.setSpecId((Long)id);
                specificationOptionMapper.delete(so);
                specificationMapper.deleteByPrimaryKey(id);
            }
        }catch(Exception ex){
            throw new RuntimeException(ex);
        }

    }

    /**
     * 根据主键id查询
     *
     * @param id
     */
    @Override
    public Specification findOne(Serializable id) {
        return null;
    }

    /**
     * 查询全部
     */
    @Override
    public List<Specification> findAll() {
        return null;
    }

    /**
     * 多条件分页查询
     *
     * @param specification
     * @param page
     * @param rows
     */
    @Override
    public PageResult findByPage(Specification specification, int page, int rows) {
        try {
            //分页
            PageInfo<Specification> pageInfo =PageHelper.startPage(page,rows).doSelectPageInfo(new ISelect() {
                @Override
                public void doSelect() {
                     specificationMapper.findAll(specification);
                }
            });

            return new PageResult(pageInfo.getTotal(),pageInfo.getList());
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 根据规格主键查询规格选项
     *
     * @param id
     */
    @Override
    public List<SpecificationOption> findSpecOption(Long id) {
        try {
            // 创建规格选项对象封装查询条件
            SpecificationOption so = new SpecificationOption();
            so.setSpecId(id);
            return specificationOptionMapper.select(so);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

    }

    /**
     * 查询所有的规格(id与specName)
     */
    @Override
    public List<Map<String, Object>> findAllByIdAndName() {

        try {
            return specificationMapper.findAllByIdAndName();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}
